home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1372 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.8 KB  |  89 lines

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c++,comp.sys.mac.programmer.help,comp.sys.mac.oop.misc,comp.sys.mac.oop.tcl
  4. Subject: Re: [Q] Overloading operator ->
  5. Date: 10 Jan 1996 18:13:53 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan10131353@g7240065.bridge.bst.bls.com>
  8. References: <mlj-0901960854360001@urals.jpl.nasa.gov>
  9. NNTP-Posting-Host: bstfirewall.bst.bls.com
  10. In-reply-to: mlj@tazboy.jpl.nasa.gov's message of Tue, 09 Jan 1996 08:54:36
  11.     -0800
  12.  
  13. In article <mlj-0901960854360001@urals.jpl.nasa.gov> mlj@tazboy.jpl.nasa.gov (Mose L. Johnson) writes:
  14.  
  15. :   One of the things I want to do is overload the arrow(->) operator.  Here
  16. :   is an example of what I tried and want to do.
  17.  
  18. :   template <class T> class ref<class T>
  19.  
  20. this should be
  21.     template <class T> class ref
  22. the <class T> at the end is unnecessary and the compiler should complain
  23. about it.
  24.  
  25. :   {
  26. :      public:
  27. :      T* operator ->();
  28. :   };
  29.  
  30. :   class tester
  31. :   {
  32. :      public:
  33. :      void member();
  34. :   };
  35.  
  36. :   void main()
  37.  
  38. this should be
  39.     int main(void)
  40.  
  41. :   {
  42. :      ref<tester *> var;
  43.  
  44. this should be
  45.        ref<tester> var;
  46.  
  47. :      var->member();
  48. :   }
  49.  
  50. :   When I try to compile this, my compiler gives me an error of:
  51. :      illegal return type for operator->()
  52.  
  53. There are a couple of things missing like a constructor which takes a
  54. parameter of type tester* and a attribute of that type in the template class
  55. Like:
  56.  
  57.   template <class T>
  58.   class ref
  59.   {
  60.     public:
  61.       ref(T* t) : t_(t)
  62.       { }
  63.       T* operator ->()
  64.       { return t_; }
  65.  
  66.     private:
  67.       T* t_;
  68.   };
  69.  
  70.   class tester
  71.   {
  72.     public:
  73.       void member();
  74.   };
  75.  
  76.   int
  77.   main(void)
  78.   {
  79.       ref<tester> var(new tester);
  80.       var->member();
  81.   }
  82.  
  83. Hope this helps
  84. Regards
  85.  
  86.    -A.
  87. -- 
  88. | A.Champion                |
  89.